home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.01 Apr⁄May 92 / Microsecond Timer / MicrosecondTimerDemo.c < prev   
Encoding:
C/C++ Source or Header  |  1991-08-05  |  3.8 KB  |  173 lines  |  [TEXT/KAHL]

  1. /*                    MicrosecondTimerDemo.c                */
  2. /*
  3.  * This is a totally useless demo program that scribbles
  4.  * the time of day as quickly as it can, then it tries
  5.  * to determine the cost of calling the time routines.
  6.  * Click on the mouse whenever you get bored.
  7.  */
  8. #include <stdio.h>
  9. #include <console.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include "MicrosecondTimer.h"
  13. #define MILLION        (1000000L)
  14. #define HIST_MAX    100
  15. #define    HIST_SIZE    (100L)    /* 100 microseconds per bin    */
  16. double    histogram[HIST_MAX];
  17. double    overflow;
  18.  
  19. double    DoubleDelta(
  20.         MicrosecondEpoch    *start,
  21.         MicrosecondEpoch    *finish
  22.     );
  23.  
  24. main(
  25.         int            argc,
  26.         char        **argv
  27.     )
  28. {
  29.         MicrosecondEpoch        then;
  30.         MicrosecondEpoch        now;
  31.         MicrosecondEpoch        start;
  32.         MicrosecondEpoch        finish;
  33.         long                    elapsed;
  34.         long                    smallest;
  35.         long                    biggest;
  36.         double                    trials;
  37.         double                    sum, sumSq;
  38.         double                    mean, variance, sd;
  39.         double                    seconds;
  40.         double                    trialTime;
  41.         register short            i,top;
  42.         Str255                    message;
  43.  
  44.         /*
  45.          * Your program may crash if you call Think C
  46.          * stdio routines before initializing the
  47.          * timer module.
  48.          */
  49.         InitializeMicrosecondTimer();
  50.         argc = ccommand(&argv);
  51.         printf("Hit the mouse to stop\n");
  52.         GetEpoch(&then);
  53.         while (Button() == FALSE) {
  54.             SystemTask();
  55.             GetEpoch(&now);
  56.             EpochToString(&now, message);
  57.             DeltaTime(&then, &now, &elapsed);
  58.             printf(
  59.                 "%#s - %ld.%06ld\n",
  60.                 message,
  61.                 elapsed / MILLION,
  62.                 elapsed % MILLION
  63.             );
  64.             then = now;
  65.         }
  66.         while (Button())
  67.             ;
  68.         printf("Beginning timer resolution test.\n");
  69.         printf("Hit the mouse to stop\n");
  70.         smallest = LONG_MAX;
  71.         biggest = 0;
  72.         trials = 0;
  73.         sum = sumSq = 0;
  74.         i = 0;
  75.         GetEpoch(&start);
  76.         while (Button() == FALSE) {
  77.             if ((++i % 1000) == 0)
  78.                 SystemTask();
  79.             GetEpoch(&then);
  80.             GetEpoch(&now);
  81.             DeltaTime(&then, &now, &elapsed);
  82.             if (elapsed < smallest)
  83.                 smallest = elapsed;
  84.             if (elapsed > biggest)
  85.                 biggest = elapsed;
  86.             sum += elapsed;
  87.             sumSq += (elapsed * elapsed);
  88.             trials++;
  89.             i = elapsed / HIST_SIZE;
  90.             if (i >= HIST_MAX)
  91.                 ++overflow;
  92.             else {
  93.                 ++histogram[i];
  94.             }
  95.         }
  96.         while (Button())
  97.             ;
  98.         GetEpoch(&finish);
  99.         top = HIST_MAX;
  100.         if (overflow == 0) {
  101.             while (top > 0 && histogram[top - 1] == 0)
  102.                 --top;
  103.         }
  104.         printf("Each histogram bucket contains %ld µsec.\n",
  105.             HIST_SIZE);
  106.         for (i = 0; i < top; i++) {
  107.             printf("%5ld: %.0f\n",
  108.                 i * HIST_SIZE, histogram[i]);
  109.         }
  110.         if (overflow > 0)
  111.             printf("%.0f overflow\n");
  112.         printf("Timer minimum = %ld.%06ld,",
  113.             smallest / MILLION,
  114.             smallest % MILLION
  115.         );
  116.         printf(" maximum = %ld.%06ld\n",
  117.             biggest / MILLION,
  118.             biggest % MILLION
  119.         );
  120.         printf("%.0f trials", trials);
  121.         if (trials > 0) {
  122.             mean = sum / trials;
  123.             printf(": mean %.2f µsec.", mean);
  124.             if (trials > 1.0)  {
  125.                 /*
  126.                  * No, this is not the proper way to
  127.                  * calculate the variance.
  128.                  */
  129.                 variance = (sumSq * trials) - (sum * sum);
  130.                 variance /= (trials * trials);
  131.                 sd = sqrt(variance);    /* Std. deviation    */
  132.                 printf(", variance %0.2f,", variance);
  133.                 printf(" standard deviation %0.2f", sd);
  134.             }
  135.         }
  136.         printf("\n");
  137.         seconds = DoubleDelta(&start, &finish);
  138.         printf("%0.3f seconds", seconds);
  139.         if (seconds > 0.0)
  140.             printf(", %0.2f trials/sec.", trials / seconds);
  141.         if (trials > 0.0) {
  142.             trialTime = (seconds * (double) MILLION) / trials;
  143.             printf(", %.2f µsec./trial\n", trialTime);
  144.             printf("%.2f µsec. mean test overhead",
  145.                 trialTime - mean
  146.             );
  147.         }
  148.         printf("\n");
  149. }
  150.  
  151. /*
  152.  * Compute the time difference as a double-precision
  153.  * number of seconds.
  154.  */
  155. double
  156. DoubleDelta(
  157.         MicrosecondEpoch    *start,
  158.         MicrosecondEpoch    *finish
  159.     )
  160. {
  161.         double                seconds;
  162.         double                microseconds;
  163.         double                result;
  164.         
  165.         seconds = finish->time - start->time;
  166.         microseconds =
  167.             finish->microsecond - start->microsecond;
  168.         result = seconds
  169.                + (microseconds / (double) MILLION);
  170.         return (result);
  171. }
  172.  
  173.